<template>
  <div class="containerWrap">
    <el-container class="container">
      <el-main class="rightMain">
        <el-container>
          <el-header class="dzy-main-header">
            <div class="btn-group-el">
              <btn-group :currentSelectedData="currentSelectedData" @btnClickCallback="btnClickCallback"></btn-group>
            </div>
            <div class="dzy-custom-search-box">
              <div>
                单位：<el-input size="small" placeholder="请输入名称" v-model="queryData.name" clearable class="dzy-custom-input"></el-input>
              </div>
              <div>
                负责人：<el-input size="small" placeholder="请输入负责人" v-model="queryData.responsibility" clearable class="dzy-custom-input"></el-input>
              </div>
              <div>
                负责人手机：<el-input size="small" placeholder="请输入负责手机" v-model="queryData.phone" clearable class="dzy-custom-input"></el-input>
              </div>
              <div>
                是否委托人：
                <el-select filterable size="small"  style="width: 125px;" clearable v-model="queryData.isClient" placeholder="是否委托人">
                  <el-option :value="1" label="是"></el-option>
                  <el-option :value="0" label="否"></el-option>
                </el-select>
              </div>
              <div>
                <el-button size="small" @click="searchHandleClick" type="primary" icon="el-icon-search">查询</el-button>
              </div>
            </div>
          </el-header>
          <el-main class="tableMain">
            <app-table :page-data="pageData"
                       :table-columns="tableColumns"
                       v-on:handleCheckedRows="handleCheckedRows"
                       v-on:currentChange="handleCurrentChange"/>
            <el-dialog :title="dialogTitle"
                       :visible.sync="dialogFormVisible"
                       :close-on-click-modal="false"
                       class="dialog_style"
                        :before-close="close">
              <div class="formWrapper">
                <el-form :model="formData"
                         :rules="rules"
                         ref="formData"
                         label-width="95px"
                         size="small">
                  <el-divider content-position="left">基本信息</el-divider>
                  <el-row :gutter="15">
                    <el-col :span="12">
                      <el-form-item label="单位名称：" prop="name">
                        <el-input placeholder="单位名称" v-model="formData.name"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="是否委托人：" prop="isClient">
                        <el-select filterable style="width: 100%;" clearable v-model="formData.isClient" placeholder="状态">
                          <el-option :key="0" :value="1" label="是"></el-option>
                          <el-option :key="1" :value="0" label="否"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="证件类型：" prop="certificatesType">
                        <el-select filterable style="width: 100%;" clearable v-model="formData.certificatesType" placeholder="证件类型">
                          <el-option v-for="item in cardOption"
                            :key="item.id"
                            :value="item.value"
                            :label="item.name"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="证件号码：" prop="certificatesCode">
                        <el-input placeholder="证件号码" v-model="formData.certificatesCode"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="所属行业：" prop="industry">
                        <el-input placeholder="所属行业" v-model="formData.industry"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="办公地址：" prop="workAddress">
                        <el-input placeholder="办公地址" v-model="formData.workAddress"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="负责人姓名：" prop="responsibility">
                        <el-input placeholder="负责人姓名" v-model="formData.responsibility"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="负责人手机：" prop="phone">
                        <el-input placeholder="负责人手机" v-model="formData.phone"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="选择经纬度：" label-width="90px" prop="longitude">
                        <el-row>
                          <el-col :span="10">
                          <el-input v-model="formData.longitude" :disabled="true" placeholder="经度" ></el-input>
                          </el-col>
                          <el-col :span="1" style="text-align: center">-</el-col>
                          <el-col :span="10">
                            <el-input v-model="formData.latitude" :disabled="true" placeholder="纬度" ></el-input>
                          </el-col>
                          <el-col :span="3">
                            <el-button type="text" @click="openSimpleMap">&nbsp;&nbsp;从地图选择</el-button>
                          </el-col>
                        </el-row>
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="备注：" prop="remark">
                        <el-input placeholder="备注" type='textarea' :rows='5' v-model="formData.remark"></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
              <div v-if="!showOnly" slot="footer" class="dialog-footer">
                <el-button @click="cancel('formData')">取 消</el-button>
                <el-button type="primary" @click="save('formData')">提 交</el-button>
              </div>
            </el-dialog>
            <el-dialog class="dialog-body-mini dialog-body-map" :visible.sync="showMap" title="地图标记" width="70%" top="8vh" :before-close="mapCancel">
              <edit-map v-if="showMap" :mode="mapMode" @appMapCallback="appMapCallback" :target="mapTarget" :row-id="currentSelectedRowId"></edit-map>
            </el-dialog>
          </el-main>
        </el-container>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import AppTree from '@/components/common/AppTree'
import BtnGroup from '@/components/BtnGroup'
import AppTable from '@/components/AppTable'
import AppUpload from '@/components/AppUpload'
import EditMap from '@/components/EditMap'
import {getDictDataOption} from '@/utils/getDictDataOption'
import {
  createPlaintiffUnit,
  findPlaintiffUnit,
  updatePlaintiffUnit,
  deletePlaintiffUnit,
  getPlaintiffUnit
} from '@/http/kehuApi'
export default {
  data () {
    return {
      mapMode: 'full',
      dialogTitle: '',
      currentSelectedRowId: '',
      showOnly: false,
      showMap: false,
      dialogFormVisible: false,
      treeData: [],
      mapTarget: {},
      queryData: {},
      pageData: {},
      formData: {},
      cardOption: [],
      genderOption: [],
      nationOption: [],
      currentSelectedData: [],
      tableColumns: [
        { label: '单位名称', prop: 'name' },
        { label: '是否委托人', prop: 'isClient', formatter: this.clientFormatter },
        { label: '负责人', prop: 'esponsibility' },
        { label: '负责人手机', prop: 'phone' },
        { label: '证件号', prop: 'certificatesCode' },
        { label: '创建时间', prop: 'createTime', formatter: this.dateFormatter },
        { label: '备注', prop: 'remark' }
      ],
      rules: {
        name: [{ required: true, message: '请输入单位名称', trigger: 'blur' }],
        isClient: [{ required: true, message: '是否委托人', trigger: 'change' }]
      },
      dataPickerBeforeTodayOptions: {
        disabledDate (time) {
          return time.getTime() > Date.now()
        }
      }
    }
  },
  mounted () {
    this.getTableData()
    this.cardOption = getDictDataOption('证件代码')
  },
  components: {
    AppTree,
    BtnGroup,
    AppTable,
    AppUpload,
    EditMap},
  methods: {
    // 时间格式化
    dateFormatter (row) {
      if (row && row.createTime) {
        return this.$moment(row.createTime).format('YYYY-MM-DD')
      }
    },
    clientFormatter (row) {
      if (row.status === 1) {
        return '是'
      } else if (row.status === 0) {
        return '否'
      }
    },
    btnClickCallback (callbackData) {
      if (callbackData.btnType === 'delete') {
        this.dialogFormVisible = false
        let rows = callbackData.rows
        this.del(rows)
      } else {
        if (callbackData.btnType === 'post') {
        } else {
          let data = callbackData.rows[0]
          this.getSingle(data)
        }
        this.showOnly = callbackData.showOnly
        this.dialogTitle = callbackData.dialogTitle
        this.dialogFormVisible = true
      }
    },
    save (formName) {
      this.$refs[formName].validate((valid) => {
        if (!valid) {
          this.$alert('请填写正确表单格式', '', {
            confirmButtonText: '确定'
          })
          return ''
        } else {
          if (this.formData.id) {
            updatePlaintiffUnit(this.formData).then(resp => {
              if (resp.code && resp.code === '200') {
                this.getTableData()
                this.cancel(formName)
                this.$message({
                  showClose: true,
                  message: '修改成功',
                  type: 'success'
                })
              }
            })
          } else {
            console.log(this.formData)
            createPlaintiffUnit(this.formData).then(resp => {
              if (resp.data && resp.code === '200') {
                this.$message({
                  type: 'success',
                  message: '添加成功'
                })
                this.cancel(formName)
                this.getTableData()
              }
            })
          }
        }
      })
    },
    del (rows) {
      if (rows && rows instanceof Array && rows.length > 0) {
        this.$confirm('此操作将永久删除，是否继续？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          rows.forEach(row => {
            deletePlaintiffUnit(row).then(resp => {
              if (resp.code && resp.code === '200') {
                this.getTableData()
                this.$message({
                  showClose: true,
                  message: '删除成功',
                  type: 'success'
                })
              }
            })
          })
        })
      }
    },
    cancel (formName) {
      this.formData = {}
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    close () {
      this.formData = {}
      this.dialogFormVisible = false
    },
    getTableData () {
      let param = this.queryData
      findPlaintiffUnit(param).then(resp => {
        if (resp.data && resp.code === '200') {
          this.pageData = resp.data
        }
      })
    },
    // 搜索按钮操作
    searchHandleClick () {
      this.queryData.offset = 0
      this.getTableData()
    },
    handleCheckedRows (rows) {
      if (rows && rows instanceof Array && rows.length > 0) {
        this.currentSelectedData = rows
      } else {
        this.currentSelectedData = []
      }
    },
    handleCurrentChange (val) {
      this.queryData.offset = (val - 1) * 10
      this.getTableData()
    },
    // 查看单条活动分类详细信息
    getSingle (data) {
      getPlaintiffUnit(data).then(resp => {
        if (resp.data && resp.code === '200') {
          this.formData = resp.data
        }
      })
    },
    openSimpleMap () {
      this.mapMode = 'simple'
      this.showMap = true
      this.mapHackReset = true
    },
    appMapCallback (appMapInfo) {
      if (appMapInfo.target) {
        if (appMapInfo.target.targetId) {
          let mapLocation = {}
          if (appMapInfo.poly && appMapInfo.poly instanceof Array && appMapInfo.poly.length > 0) {
            let length = appMapInfo.poly.length
            this.saveMapLocationInfoByArray(0, length, appMapInfo)
          } else {
            mapLocation = this.appMapInfo2MapLocation(appMapInfo)
            this.saveMapLocationInfo(mapLocation)
          }
        } else {
          this.showOnly = false
          this.showMap = false
          this.$set(this.formData, 'latitude', appMapInfo.lnglat.lat)
          this.$set(this.formData, 'longitude', appMapInfo.lnglat.lng)
        }
      }
      return ''
    },
    mapCancel () {
      this.mapHackReset = false
      this.showMap = false
    }
  }
}
</script>
<style scoped>
  .containerWrap {
    padding:10px;
    height: 100%;
    box-sizing: border-box
  }
  .container {
    height: 100%;
  }
</style>
